package com.yuyou.activity.group.api.controller;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.yuyou.activity.comm.service.pay.ZyCreateOrderNotFail;
import com.yuyou.activity.comm.service.pay.ZyRefundOrderNotFail;
import com.yuyou.activity.comm.service.tool.MailUtil;
import com.yuyou.activity.comm.service.vo.req.ReqCreateOrderNotice;
import com.yuyou.activity.comm.service.vo.req.ReqRefundOrderNotice;
import com.yuyou.activity.group.service.service.IActivitySPNoticeService;
import com.yuyou.activity.util.bean.ResBoolSimpleInfo;
import com.yuyou.activity.util.enums.ZyPayEnums;
import com.yuyou.activity.util.util.LogUtil;
import com.yuyou.activity.util.util.ZyPaySignUtil;


@Validated
@RestController
@RequestMapping(value="/api/activityGroupSPNot")
public class ActivityGroupSPNoticeController extends BaseController{
	

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Value("${appId}")
	private String appId;
	
	@Value("${appKey}")
	private String appKey;
	
	@Autowired
	private IActivitySPNoticeService activitySPNoticeService;
		
	@Autowired
	private MailUtil mailUtil;
	
	private Map<String, String> TransMap(HttpServletRequest request) {
		Map<String, String> params = new HashMap<>();
		Enumeration<?> enu = request.getParameterNames();
		while (enu.hasMoreElements()) {
			String paraName = (String) enu.nextElement();
			params.put(paraName, request.getParameter(paraName));
		}
		LogUtil.info(logger,JSON.toJSONString(params));
		return params;
	}
	
	@RequestMapping(value = "/order/notice", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String orderNotice(HttpServletRequest request)  {
		String orderId=StringUtils.EMPTY;
		try {
			LogUtil.info(logger, "order notice");
			Map<String, String> params = TransMap(request);
			String _sign = ZyPaySignUtil.sign(params, appKey);
			if (_sign.equals(params.get("sign"))) {
				ReqCreateOrderNotice notice = new ReqCreateOrderNotice();
				notice.setTradeSn(params.get("id"));
				notice.setAttach(params.get("attach"));
				notice.setOrderId(params.get("orderId"));
				orderId=params.get("orderId");
				notice.setPayOrderId(params.get("payOrderId"));
				notice.setPayStatus(params.get("payStatus"));
				notice.setZyRequest(JSON.toJSONString(params));
				ResBoolSimpleInfo<String> res=activitySPNoticeService.createOrderNotice(notice);
				return res.getData();
			} else {
				return ZyCreateOrderNotFail
						.getRes(ZyPayEnums.payApplyNoticeCode._4011.getKey());
			}
		} catch (Exception e) {
			mailUtil.sendEmail("GroupOrderNoticeError", orderId+"");
			return null;
		}
	}
	
	@RequestMapping(value = "/refund/notice", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String refundNotice(HttpServletRequest request)  {
		String refundId=StringUtils.EMPTY;
		try {
			LogUtil.info(logger, "refund notice");
			Map<String, String> params = TransMap(request);
			String _sign = ZyPaySignUtil.sign(params, appKey);
			if (_sign.equals(params.get("sign"))) {

				ReqRefundOrderNotice notice = new ReqRefundOrderNotice();
				notice.setTradeSn(params.get("id"));
				notice.setRefundId(params.get("refundId"));
				refundId=params.get("refundId");
				notice.setRefundOrderId(params.get("refundOrderId"));
				notice.setRefundAmount(params.get("refundAmount"));
				notice.setRefundStatus(params.get("refundStatus"));
				notice.setAttach(params.get("attach"));
				notice.setZyRequest(JSON.toJSONString(params));			
				ResBoolSimpleInfo<String> res=activitySPNoticeService.refundNotice(notice);
				return res.getData();
			} else {
				return ZyRefundOrderNotFail
						.getRes(ZyPayEnums.payApplyNoticeCode._4011.getKey());
			}
		} catch (Exception e) {
			mailUtil.sendEmail("GroupRefundNoticeError", refundId+"");
			return null;
		}
		
	}
	
}
